package net.neptech.listener.impl;

import lombok.extern.slf4j.Slf4j;
import net.neptech.listener.AbstractEventListenerStrategy;
import net.neptech.service.RemoteService;
import net.neptech.util.FlowableUtil;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.impl.FlowableActivityEventImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Saya
 * @Date 2025/8/22 14:46
 * @Description 实例节点开始
 * @Params
 * @Return
 **/

@Slf4j
@Component
public class ActivityStartedEventListener extends AbstractEventListenerStrategy implements InitializingBean {
    @Autowired
    private RemoteService remoteService;
    /**
     * 处理数据
     */
    @Override
    public void handle(FlowableEvent event) {
        //节点开始执行
        FlowableActivityEventImpl flowableActivityEvent = (FlowableActivityEventImpl) event;
        String activityId = flowableActivityEvent.getActivityId();
        String activityName = flowableActivityEvent.getActivityName();
        DelegateExecution execution = flowableActivityEvent.getExecution();
        String tenantId = execution.getTenantId();

        String executionId = flowableActivityEvent.getExecutionId();
        log.info("节点开始  节点id：{} 名字:{} executionId:{}", activityId, activityName, executionId);


        String processInstanceId = flowableActivityEvent.getProcessInstanceId();

        String processDefinitionId = flowableActivityEvent.getProcessDefinitionId();
        String flowId = FlowableUtil.getFlowId(processDefinitionId);

        DelegateExecution parent = execution.getParent();
        if (parent != null && parent.isMultiInstanceRoot()) {
            //上级是多实例
        } else {
            saveStartEventContent(flowId, processInstanceId, activityId, activityName, execution.getId(), tenantId,
                    execution.getParentId());
        }
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(FlowableEngineEventType.ACTIVITY_STARTED.toString());
    }
}
